/**
 * 
 */
package ro.utcn.tp.taskManagement.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.DateUtil;
import com.toedter.calendar.JDateChooser;

import ro.utcn.tp.taskManagement.business.ImageService;
import ro.utcn.tp.taskManagement.business.TasksManager;
import ro.utcn.tp.taskManagement.business.UsersManager;
import ro.utcn.tp.taskManagement.model.ImageDefs;
import ro.utcn.tp.taskManagement.model.ImagePanel;
import ro.utcn.tp.taskManagement.model.Task;
import ro.utcn.tp.taskManagement.model.User;
import ro.utcn.tp.taskManagement.utils.DateUtils;

/**
 * @author Coni
 * 
 */
public class UserPage implements ActionListener {
	private JLabel jlbTitlu;
	private JLabel jlbDate;
	private JLabel jlbDummy;
	private JButton jbnAddTask;
	private JButton jbnEditTask;
	private JButton jbnDeleteTask;
	private JButton jbnExportTasks;
	private JButton jbnSearchTasks;
	private JButton jbnDoneTask;
	private JButton jbnCancelTask;
	private JButton jbnReopenTask;
	private JButton jbnStartProgress;
	private JButton jbnStopProgress;
	private JButton jbnPrevious;
	private JButton jbnNext;
	private JButton jbnBack;
	private JDateChooser jdcCalendar;
	private JButton jbnShowAllTasks;

	private JTable jtbTasks;
	private DefaultTableModel jtbTableModel;

	private static UserPage instance;
	private ImageService imageService;

	private Container container;

	private boolean newUser;
	private User user;
	private List<Task> tasks;

	public UserPage(User user) {
		this.user = user;
		imageService = ImageService.getInstance();// instantiez imageservice
		newUser = true;
		createGUI();
	}

	public void createGUI() {

		container = new ImagePanel(
				imageService.getImage(ImageDefs.APPLICATION_BACKGROUND_PATH));
		container.setLayout(new GridBagLayout());
		arrangeComponents();
	}

	public void arrangeComponents() {
		jlbTitlu = new JLabel("My tasks");
		jlbDate = new JLabel("Select a date to show your's tasks:");
		jlbDummy= new JLabel("      ");
		jbnAddTask = new JButton("Add task ");
		jbnEditTask = new JButton("Edit task");
		jbnDeleteTask = new JButton("Delete task");
		jbnExportTasks = new JButton("Export");
		jbnSearchTasks = new JButton("Search");
		jbnDoneTask = new JButton("Task done");
		jbnCancelTask = new JButton("Cancel task");
		jbnReopenTask = new JButton("Reopen task");
		jbnStartProgress = new JButton("Start progress");
		jbnStopProgress = new JButton("Stop progress");
		jbnBack = new JButton("Log out");
		jbnShowAllTasks = new JButton("Show all tasks");
		jbnPrevious = new JButton("<<");
		jbnNext = new JButton(">>");
		jdcCalendar = new JDateChooser(new Date());

		jtbTableModel = new DefaultTableModel(new String[] { "Description",
				"Status", "Schedule date", "Closing date" }, 0);
		jtbTasks = new JTable(jtbTableModel) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {
				return false;
			};
		};
		jtbTasks.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent event) {
						int selectedRow = jtbTasks.getSelectedRow();
						if (selectedRow >= 0) {
							Task task = tasks.get(selectedRow);
							if (task.getStatus().equals(Task.TO_DO_STATUS)) {
								jbnStartProgress.setEnabled(true);
								jbnStopProgress.setEnabled(false);
								jbnDoneTask.setEnabled(true);
								jbnCancelTask.setEnabled(true);
								jbnReopenTask.setEnabled(false);
							} else if (task.getStatus().equals(
									Task.IN_PROGRESS_STATUS)) {
								jbnStartProgress.setEnabled(false);
								jbnStopProgress.setEnabled(true);
								jbnDoneTask.setEnabled(true);
								jbnCancelTask.setEnabled(true);
								jbnReopenTask.setEnabled(false);
							} else if (task.getStatus()
									.equals(Task.DONE_STATUS)) {
								jbnStartProgress.setEnabled(false);
								jbnStopProgress.setEnabled(false);
								jbnDoneTask.setEnabled(false);
								jbnCancelTask.setEnabled(false);
								jbnReopenTask.setEnabled(true);

							} else if (task.getStatus().equals(
									Task.CANCELED_STATUS)) {
								jbnStartProgress.setEnabled(true);
								jbnStopProgress.setEnabled(false);
								jbnDoneTask.setEnabled(false);
								jbnCancelTask.setEnabled(false);
								jbnReopenTask.setEnabled(true);
							}
						} else {
							jbnAddTask.setEnabled(true);
							jbnEditTask.setEnabled(true);
							jbnDeleteTask.setEnabled(true);
							jbnExportTasks.setEnabled(true);
							jbnSearchTasks.setEnabled(true);
							jbnDoneTask.setEnabled(true);
							jbnCancelTask.setEnabled(true);
							jbnReopenTask.setEnabled(true);
							jbnStartProgress.setEnabled(true);
							jbnStopProgress.setEnabled(true);
							jbnBack.setEnabled(true);
							jbnShowAllTasks.setEnabled(true);
						}

						// System.out.println(jtbTasks.getValueAt(jtbTasks.getSelectedRow(),
						// 0).toString());

					}
				});

		JScrollPane scrollpane = new JScrollPane(jtbTasks);
		scrollpane.setWheelScrollingEnabled(true);
		jtbTasks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		Font font = new Font("Times New Roman", Font.ITALIC, 30);
		jlbTitlu.setFont(font);
		Font font1 = new Font("Times New Roman", Font.ITALIC, 20);
		jlbDate.setFont(font1);

		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
		gridBagConstraintsx01.gridx = 2;
		gridBagConstraintsx01.gridy = 0;
		gridBagConstraintsx01.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx01.gridwidth = 9;// latimea e de 2 patratele din wigh
		gridBagConstraintsx01.fill = GridBagConstraints.BOTH;
		gridBagConstraintsx01.anchor = GridBagConstraints.CENTER;
		container.add(jlbTitlu, gridBagConstraintsx01);

		GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
		gridBagConstraintsx03.gridx = 0;
		gridBagConstraintsx03.gridy = 1;
		gridBagConstraintsx03.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx03.gridwidth = 4;// latimea e de 2 patratele din wigh
		gridBagConstraintsx03.fill = GridBagConstraints.BOTH;
		gridBagConstraintsx03.anchor = GridBagConstraints.CENTER;
		container.add(jlbDate, gridBagConstraintsx03);
		
		GridBagConstraints gridBagConstraintsx23 = new GridBagConstraints();
		gridBagConstraintsx23.gridx = 3;
		gridBagConstraintsx23.gridy = 1;
		gridBagConstraintsx23.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx23.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx23.fill = GridBagConstraints.BOTH;
		container.add(jlbDummy, gridBagConstraintsx23);

		GridBagConstraints gridBagConstraintsx21 = new GridBagConstraints();
		gridBagConstraintsx21.gridx = 4;
		gridBagConstraintsx21.gridy = 1;
		gridBagConstraintsx21.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx21.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx21.fill = GridBagConstraints.BOTH;
		container.add(jbnPrevious, gridBagConstraintsx21);

		GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
		gridBagConstraintsx02.gridx = 5;
		gridBagConstraintsx02.gridy = 1;
		gridBagConstraintsx02.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx02.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
		container.add(jdcCalendar, gridBagConstraintsx02);

		GridBagConstraints gridBagConstraintsx22 = new GridBagConstraints();
		gridBagConstraintsx22.gridx = 7;
		gridBagConstraintsx22.gridy = 1;
		gridBagConstraintsx22.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx22.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx22.fill = GridBagConstraints.BOTH;
		container.add(jbnNext, gridBagConstraintsx22);

		GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
		gridBagConstraintsx07.gridx = 0;
		gridBagConstraintsx07.gridy = 2;
		gridBagConstraintsx07.gridwidth = 6;
		gridBagConstraintsx07.insets = new Insets(10, 10, 10, 10);
		gridBagConstraintsx07.weightx = 1;
		gridBagConstraintsx07.weighty = 1;
		gridBagConstraintsx07.fill = GridBagConstraints.BOTH;

		container.add(scrollpane, gridBagConstraintsx07);

		GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
		gridBagConstraintsx08.gridx = 0;
		gridBagConstraintsx08.gridy = 3;
		gridBagConstraintsx08.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx08.gridwidth = 1;// latimea e de 1 patratele din wigh
		gridBagConstraintsx08.fill = GridBagConstraints.BOTH;
		container.add(jbnAddTask, gridBagConstraintsx08);

		GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
		gridBagConstraintsx09.gridx = 1;
		gridBagConstraintsx09.gridy = 3;
		gridBagConstraintsx09.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx09.gridwidth = 1;
		gridBagConstraintsx09.fill = GridBagConstraints.BOTH;
		container.add(jbnEditTask, gridBagConstraintsx09);

		GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
		gridBagConstraintsx10.gridx = 2;
		gridBagConstraintsx10.gridy = 3;
		gridBagConstraintsx10.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx10.gridwidth = 1;
		gridBagConstraintsx10.fill = GridBagConstraints.BOTH;
		container.add(jbnDeleteTask, gridBagConstraintsx10);

		GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
		gridBagConstraintsx11.gridx = 0;
		gridBagConstraintsx11.gridy = 4;
		gridBagConstraintsx11.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx11.gridwidth = 1;
		gridBagConstraintsx11.fill = GridBagConstraints.BOTH;
		container.add(jbnStartProgress, gridBagConstraintsx11);

		GridBagConstraints gridBagConstraintsx12 = new GridBagConstraints();
		gridBagConstraintsx12.gridx = 1;
		gridBagConstraintsx12.gridy = 4;
		gridBagConstraintsx12.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx12.gridwidth = 1;
		gridBagConstraintsx12.fill = GridBagConstraints.BOTH;
		container.add(jbnStopProgress, gridBagConstraintsx12);

		GridBagConstraints gridBagConstraintsx13 = new GridBagConstraints();
		gridBagConstraintsx13.gridx = 0;
		gridBagConstraintsx13.gridy = 5;
		gridBagConstraintsx13.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx13.gridwidth = 1;
		gridBagConstraintsx13.fill = GridBagConstraints.BOTH;
		container.add(jbnExportTasks, gridBagConstraintsx13);

		GridBagConstraints gridBagConstraintsx14 = new GridBagConstraints();
		gridBagConstraintsx14.gridx = 2;
		gridBagConstraintsx14.gridy = 4;
		gridBagConstraintsx14.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx14.gridwidth = 1;
		gridBagConstraintsx14.fill = GridBagConstraints.BOTH;
		container.add(jbnDoneTask, gridBagConstraintsx14);

		GridBagConstraints gridBagConstraintsx15 = new GridBagConstraints();
		gridBagConstraintsx15.gridx = 3;
		gridBagConstraintsx15.gridy = 4;
		gridBagConstraintsx15.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx15.gridwidth = 2;
		gridBagConstraintsx15.fill = GridBagConstraints.BOTH;
		container.add(jbnCancelTask, gridBagConstraintsx15);

		GridBagConstraints gridBagConstraintsx16 = new GridBagConstraints();
		gridBagConstraintsx16.gridx = 5;
		gridBagConstraintsx16.gridy = 4;
		gridBagConstraintsx16.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx16.gridwidth = 1;
		gridBagConstraintsx16.fill = GridBagConstraints.BOTH;
		container.add(jbnReopenTask, gridBagConstraintsx16);

		GridBagConstraints gridBagConstraintsx17 = new GridBagConstraints();
		gridBagConstraintsx17.gridx = 1;
		gridBagConstraintsx17.gridy = 5;
		gridBagConstraintsx17.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx17.gridwidth = 1;
		gridBagConstraintsx17.fill = GridBagConstraints.BOTH;
		container.add(jbnSearchTasks, gridBagConstraintsx17);

		GridBagConstraints gridBagConstraintsx18 = new GridBagConstraints();
		gridBagConstraintsx18.gridx = 2;
		gridBagConstraintsx18.gridy = 5;
		gridBagConstraintsx18.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx18.gridwidth = 1;
		gridBagConstraintsx18.fill = GridBagConstraints.BOTH;
		container.add(jbnBack, gridBagConstraintsx18);

		GridBagConstraints gridBagConstraintsx19 = new GridBagConstraints();
		gridBagConstraintsx19.gridx = 3;
		gridBagConstraintsx19.gridy = 3;
		gridBagConstraintsx19.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx19.gridwidth = 2;
		gridBagConstraintsx19.fill = GridBagConstraints.BOTH;
		container.add(jbnShowAllTasks, gridBagConstraintsx19);

		jbnAddTask.setEnabled(true);
		jbnEditTask.setEnabled(true);
		jbnDeleteTask.setEnabled(true);
		jbnExportTasks.setEnabled(true);
		jbnSearchTasks.setEnabled(true);
		jbnDoneTask.setEnabled(true);
		jbnCancelTask.setEnabled(true);
		jbnReopenTask.setEnabled(true);
		jbnStartProgress.setEnabled(true);
		jbnStopProgress.setEnabled(true);
		jbnBack.setEnabled(true);
		jbnShowAllTasks.setEnabled(true);

		jbnAddTask.addActionListener(this);
		jbnEditTask.addActionListener(this);
		jbnDeleteTask.addActionListener(this);
		jbnExportTasks.addActionListener(this);
		jbnSearchTasks.addActionListener(this);
		jbnDoneTask.addActionListener(this);
		jbnCancelTask.addActionListener(this);
		jbnReopenTask.addActionListener(this);
		jbnStartProgress.addActionListener(this);
		jbnStopProgress.addActionListener(this);
		jbnBack.addActionListener(this);
		jbnShowAllTasks.addActionListener(this);
		jbnPrevious.addActionListener(this);
		jbnNext.addActionListener(this);

		jbnAddTask.setToolTipText("Add a new task");
		jbnEditTask.setToolTipText("Edit selected task's details");
		jbnDeleteTask.setToolTipText("Delete selected task");
		jbnExportTasks.setToolTipText("Export tasks from table");
		jbnSearchTasks.setToolTipText("Search tasks by description or status");
		jbnDoneTask.setToolTipText("Change task's status to DONE");
		jbnCancelTask.setToolTipText("Change task's status to CANCELED");
		jbnReopenTask.setToolTipText("Reopen selected task");
		jbnStartProgress.setToolTipText("Start task progress");
		jbnStopProgress.setToolTipText("Stop task progress");
		jbnBack.setToolTipText("Log out");
		jbnShowAllTasks.setToolTipText("Show all your tasks");

	}

	public static UserPage getNewInstance(User user) {

		instance = new UserPage(user);
		instance.displayTasks();
		return instance;
	}

	public static UserPage getNewInstance(User user, List<Task> searchedTasks) {

		instance = new UserPage(user);
		instance.displayTasks(searchedTasks);
		return instance;
	}

	public static UserPage getInstance() {
		instance.displayTasks();
		return instance;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {

			if (e.getSource() == jbnAddTask) {
				addTask();
			} else if (e.getSource() == jbnEditTask) {
				editTask();
			} else if (e.getSource() == jbnDeleteTask) {
				deleteTask();
			} else if (e.getSource() == jbnExportTasks) {
				exportTask();
			} else if (e.getSource() == jbnSearchTasks) {
				search();
			} else if (e.getSource() == jbnDoneTask) {
				doneTask();
			} else if (e.getSource() == jbnCancelTask) {
				cancelTask();
			} else if (e.getSource() == jbnReopenTask) {
				reopenTask();
			} else if (e.getSource() == jbnStartProgress) {
				startProgress();
			} else if (e.getSource() == jbnStopProgress) {
				stopProgress();
			} else if (e.getSource() == jbnBack) {
				cancel();
			} else if (e.getSource() == jbnShowAllTasks) {
				displayTasks();
			}else if(e.getSource()==jbnPrevious){
				previous();
			}else if(e.getSource()==jbnNext){
				next();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void next() {
		Calendar calendar = jdcCalendar.getCalendar();
		calendar.add(Calendar.DAY_OF_YEAR, 1);
		jdcCalendar.setCalendar(calendar);
		displayTasks();
	}

	private void previous() {
		Calendar calendar = jdcCalendar.getCalendar();
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		jdcCalendar.setCalendar(calendar);
		displayTasks();
	}

	private void startProgress() {
		int selectedRow = jtbTasks.getSelectedRow();
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			Task task = tasks.get(selectedRow);
			task.setStatus(Task.IN_PROGRESS_STATUS);
			//task.setClosedDate(null);
			TasksManager.getInstance().saveTask(task);
			displayTasks();
		}

	}

	private void stopProgress() {
		int selectedRow = jtbTasks.getSelectedRow();
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			Task task = tasks.get(selectedRow);
			task.setStatus(Task.TO_DO_STATUS);
			TasksManager.getInstance().saveTask(task);
			displayTasks();
		}

	}

	private void doneTask() {
		int selectedRow = jtbTasks.getSelectedRow();
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			Task task = tasks.get(selectedRow);
			task.setStatus(Task.DONE_STATUS);
			task.setClosedDate(DateUtils.getTodayDate());
			TasksManager.getInstance().saveTask(task);
			displayTasks();
		}

	}

	private void cancelTask() {
		int selectedRow = jtbTasks.getSelectedRow();
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			Task task = tasks.get(selectedRow);
			task.setStatus(Task.CANCELED_STATUS);
			task.setClosedDate(DateUtils.getTodayDate());
			TasksManager.getInstance().saveTask(task);
			displayTasks();
		}

	}

	private void reopenTask() {
		int selectedRow = jtbTasks.getSelectedRow();
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			Task task = tasks.get(selectedRow);
			task.setStatus(Task.IN_PROGRESS_STATUS);
			task.setClosedDate(null);
			TasksManager.getInstance().saveTask(task);
			displayTasks();
		}

	}

	public void displayTasks() {
		Calendar currentDate = jdcCalendar.getCalendar();
		currentDate =DateUtils.initializeDate(currentDate);
		List<Task> allTasks = TasksManager.getInstance().getUserTasks(user.getId(),currentDate);
		displayTasks(allTasks);
	}

	public void displayTasks(List<Task> searchedTasks) {
		tasks = searchedTasks;
		while (this.jtbTableModel.getRowCount() > 0) {
			this.jtbTableModel.removeRow(0);
		}
		Iterator<Task> iterator = searchedTasks.iterator();
		while (iterator.hasNext()) {
			Task task = iterator.next();

			SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
			String scheduleDate = sdf.format(task.getScheduleDate().getTime());
			String closedDate = "";
			if (task.getClosedDate() != null) {
				closedDate = sdf.format(task.getClosedDate().getTime());
			}

			this.jtbTableModel.addRow(new String[] { task.getDescription(),
					task.getStatus(), String.valueOf(scheduleDate),
					String.valueOf(closedDate) });

		}
	}

	private void addTask() {
		ApplicationScreen.getInstance().setContainer(
				TaskPage.getNewInstance(user).getContainer());

	}

	private void editTask() {
		int selectedRow = jtbTasks.getSelectedRow();
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}

		else {
			Task task = tasks.get(selectedRow);
			User user = UsersManager.getInstance().getUser(task.getUserId());
			TaskPage taskPage = TaskPage.getNewInstance(user, task);
			taskPage.editTask(user, task);
			ApplicationScreen.getInstance().setContainer(
					taskPage.getContainer());
			displayTasks();
		}
	}

	public JTable getJtbTasks() {
		return jtbTasks;
	}

	private void deleteTask() {
		int selectedRow = jtbTasks.getSelectedRow();
		boolean success = false;
		if (selectedRow < 0) {
			// afiseaza mesaj ca nu s-a selectat randul
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Selectati mai intai un task din tabel!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
			return;
		}

		else {
			Task task = tasks.get(selectedRow);
			success = TasksManager.getInstance().deleteTask(task);
			displayTasks();
		}
	}

	private void search() {
		ApplicationScreen.getInstance().setContainer(
				SearchPage.getNewInstance(user).getContainer());

	}

	private void exportTask() {
		ApplicationScreen.getInstance().setContainer(
				ExportPage.getNewInstance(user).getContainer());

	}

	public void cancel() {
		ApplicationScreen.getInstance().setContainer(
				LoginPage.getNewInstance().getContainer());

	}

	/*
	 * public void openSearchPage() {
	 * ApplicationScreen.getInstance().setContainer(
	 * SearchPage.getNewInstance(newUser, "").getContainer()); }
	 */

	/**
	 * @return container
	 */
	public Container getContainer() {
		return container;
	}
}
